package androidgame.teslawars;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import androidgame.framework.Game;
import androidgame.framework.Input.TouchEvent;
import androidgame.framework.gl.Camera2D;
import androidgame.framework.gl.SpriteBatcher;
import androidgame.framework.impl.GLScreen;
import androidgame.framework.math.OverlapTester;
import androidgame.framework.math.Rectangle;
import androidgame.framework.math.Vector2;
import androidgame.teslawars.game.Level.LevelListener;
import androidgame.teslawars.game.maps.Map1;
import androidgame.teslawars.game.maps.Map2;

// @author dicarlo2

public class MainMenuScreen extends GLScreen {
	Camera2D guiCam;
	SpriteBatcher batcher;
	Vector2 touchPoint;
	
	Rectangle map1Bounds;
	Rectangle map2Bounds;
	Rectangle soundBounds;
	// need to create rectangles where touch will occur for transitions (or setting sound or whatever)
	
	LevelListener levelListener;

	public MainMenuScreen(Game game) {
		super(game);
		guiCam = new Camera2D(glGraphics, 480, 320);
        batcher = new SpriteBatcher(glGraphics, 100);
        touchPoint = new Vector2(); 
        Settings.load(game.getFileIO());
        
        map1Bounds = new Rectangle(86, 145, 128, 30);
        map2Bounds = new Rectangle(266, 145, 128, 30);
        soundBounds = new Rectangle(10, 10, 216, 24);
        
		levelListener = new LevelListener() {
			public void hit(){
				;
			}
			// sounds to be played.
		};
	}

	@Override
	public void update(float deltaTime) {
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        game.getInput().getKeyEvents();
        game.getInput().getGestures();
        
        // Cycles through touchEvents and tests if any of the menu options have been hit.
        int len = touchEvents.size();
        for(int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);                        
            if(event.type == TouchEvent.TOUCH_UP) {
                touchPoint.set(event.x, event.y);
                guiCam.touchToWorld(touchPoint);
                
                if(OverlapTester.pointInRectangle(map1Bounds, touchPoint)) {
                    game.setScreen(new GameScreen(game, new Map1(levelListener))); // place holder screen
                    return;
                }
                if(OverlapTester.pointInRectangle(map2Bounds, touchPoint)) {
                	game.setScreen(new GameScreen(game, new Map2(levelListener)));
                	return;
                }
                if(OverlapTester.pointInRectangle(soundBounds, touchPoint)){
                	Settings.soundEnabled = !Settings.soundEnabled;
                }
                
                // additional overlaps should be tested to go to other screens
            }
        }
		
	}

	
	@Override
	public void present(float deltaTime) {
        GL10 gl = glGraphics.getGL();        
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        gl.glEnable(GL10.GL_TEXTURE_2D);
        
        guiCam.setViewportAndMatrices();
        
        batcher.beginBatch(Assets.background);
        batcher.drawSprite(240, 160, 480, 320, Assets.backgroundRegion);   
        batcher.endBatch(); // Draws background of main menu screen
        
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);               
        
        batcher.beginBatch(Assets.items);
        Assets.teslaWarsFont.drawText(batcher, "MAP2", 282, 160);
        Assets.teslaWarsFont.drawText(batcher, "MAP1", 102, 160);
        if (Settings.soundEnabled == false)
        	Assets.readableFont.drawText(batcher, "ENABLE SOUND", 16, 22);
        else
        	Assets.readableFont.drawText(batcher, "DISABLE SOUND", 16, 22);
        batcher.endBatch();
        
        gl.glDisable(GL10.GL_BLEND);
		
	}

	@Override
	public void pause() {
		if (Settings.soundEnabled)
			Assets.backgroundMusic.pause();
        Settings.save(game.getFileIO());		
	}

	@Override
	public void resume() {	
	}

	@Override
	public void dispose() {		
	}

}
